package com.zhengshang.api.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.zhengshang.common.annotation.Excel;
import com.zhengshang.common.core.domain.BaseEntity;

/**
 * APP交易记录对象 app_pay_record
 *
 * @author zhengshang
 * @date 2021-03-29
 */
public class AppPayRecord extends BaseEntity implements Delayed {
    public AppPayRecord() {
    }

    private static final long serialVersionUID = 1L;

    /**
     * 支付流水ID
     */
    private Long recordId;

    /**
     * 城市
     */
    private String areaName;

    /**
     * 是否快充
     */
    private Integer fast;

    /**
     * 系统内部商品Id
     */
    private Long billId;

    /**
     * 油卡Id
     */
    private Long oilcardId;

    /**
     * 商品类型
     */
    private Integer productType;


    /**
     * 支付类型（1支付宝，2微信）
     */
    @Excel(name = "支付类型", readConverterExp = "1=支付宝,2=微信")
    private String payType;

    /**
     * 购买类型（PM星级会员，PV会员，PY加油，PB话费充值）
     */
    @Excel(name = "购买类型", readConverterExp = "PM=星级会员,PV=会员,PY=加油,PB=话费充值,PC=生活缴费,OIL=油卡充值,WKY=影视充值")
    private String buyType;

    /**
     * 支付金额
     */
    @Excel(name = "支付金额")
    private BigDecimal payMoney;

    /**
     * 支付用户ID
     */
    private Long userId;

    /**
     * 手机号
     */
    @Excel(name = "手机号")
    private String phone;

    /**
     * 订单号（系统生成）
     */
    @Excel(name = "订单号")
    private String orderNum;

    /**
     * 请求参数
     */
    private String param;

    /**
     * 支付宝交易号（支付宝生成）
     */
    @Excel(name = "支付宝交易号")
    private String tradeNo;

    /**
     * 买家在支付宝的用户id
     */
    @Excel(name = "买家在支付宝的用户id")
    private String buyerUserId;

    /**
     * 状态（10支付中，20已支付，25支付失败，30已退款，40已关闭）
     */
    @Excel(name = "状态", readConverterExp = "10=支付中,20=已支付,25=支付失败,30=已退款,40=已关闭,50=交易异常（需联系客服）")
    private String status;

    @Excel(name = "充值状态", readConverterExp = "0=待充值,1=充值成功,2=充值失敗")
    private String rechargeState;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    private String delFlag;

    /**
     * 回调数据
     */
    private String callbackData;

    /**
     * 预支付交易会话标识（微信生成的预支付会话标识，用于后续接口调用中使用，该值有效期为2小时）
     */
    private String prepayId;

    /**
     * 微信支付订单号
     */
    private String transactionId;

    /**
     * 微信支付完成时间（支付完成时间，格式为yyyyMMddHHmmss，如2009年12月25日9点10分10秒表示为20091225091010）
     */
    private String timeEnd;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 轮询数据start
     */
    private long orderCreateTime;//订单创建时间（自动取消超时订单用）
    private long expTime;//订单超时时间（自动取消超时订单用）

    public Integer getProductType() {
        return productType;
    }

    public void setProductType(Integer productType) {
        this.productType = productType;
    }

    /**
     * 轮询数据end
     */


    public void setRecordId(Long recordId) {
        this.recordId = recordId;
    }

    public Long getRecordId() {
        return recordId;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public String getPayType() {
        return payType;
    }

    public String getBuyType() {
        return buyType;
    }

    public void setBuyType(String buyType) {
        this.buyType = buyType;
    }

    public void setPayMoney(BigDecimal payMoney) {
        this.payMoney = payMoney;
    }

    public BigDecimal getPayMoney() {
        return payMoney;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

    public String getOrderNum() {
        return orderNum;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getParam() {
        return param;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setBuyerUserId(String buyerUserId) {
        this.buyerUserId = buyerUserId;
    }

    public String getBuyerUserId() {
        return buyerUserId;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getRechargeState() {
        return rechargeState;
    }

    public void setRechargeState(String rechargeState) {
        this.rechargeState = rechargeState;
    }

    public String getStatus() {
        return status;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public String getCallbackData() {
        return callbackData;
    }

    public void setCallbackData(String callbackData) {
        this.callbackData = callbackData;
    }

    public String getPrepayId() {
        return prepayId;
    }

    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getTimeEnd() {
        return timeEnd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setTimeEnd(String timeEnd) {
        this.timeEnd = timeEnd;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public Integer getFast() {
        return fast;
    }

    public void setFast(Integer fast) {
        this.fast = fast;
    }

    public Long getBillId() {
        return billId;
    }

    public void setBillId(Long billId) {
        this.billId = billId;
    }

    public Long getOilcardId() {
        return oilcardId;
    }

    public void setOilcardId(Long oilcardId) {
        this.oilcardId = oilcardId;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("recordId", getRecordId())
                .append("payType", getPayType())
                .append("buyType", getBuyType())
                .append("payMoney", getPayMoney())
                .append("areaName", getAreaName())
                .append("userId", getUserId())
                .append("orderNum", getOrderNum())
                .append("fast", getFast())
                .append("billId", getBillId())
                .append("oilcardId", getOilcardId())
                .append("productType", getProductType())
                .append("tradeNo", getTradeNo())
                .append("buyerUserId", getBuyerUserId())
                .append("status", getStatus())
                .append("rechargeState", getRechargeState())
                .append("delFlag", getDelFlag())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("callbackData", getCallbackData())
                .append("prepayId", getPrepayId())
                .append("transactionId", getTransactionId())
                .append("timeEnd", getTimeEnd())
                .append("phone", getPhone())
                .append("userName", getUserName())
                .toString();
    }

    public AppPayRecord(String ordernum, Date createTime) {
        if (StringUtils.isNotBlank(ordernum)) {
            this.setOrderNum(ordernum);
        }
        if (createTime == null) {
            this.setCreateTime(DateUtils.getNowDate());
        } else {
            this.setCreateTime(createTime);
        }
        setWaitData();
    }

    /**
     * 设置轮询数据
     */
    public void setWaitData() {
        this.orderCreateTime = this.getCreateTime().getTime();
        this.expTime = DateUtils.calculationDate(DateUtils.DateType.SECOND, new Date(this.orderCreateTime), AppConstant.PAY_ORDER_OVERTIME);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.valueOf(this.expTime).compareTo(Long.valueOf(((AppPayRecord) o).expTime));
    }
}